package com.qf.xiaomi.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.xiaomi.pojo.Address;
import com.qf.xiaomi.pojo.Cart;
import com.qf.xiaomi.pojo.Order;
import com.qf.xiaomi.pojo.User;
import com.qf.xiaomi.service.AddressService;
import com.qf.xiaomi.service.CartService;
import com.qf.xiaomi.service.OrderService;
import com.qf.xiaomi.service.impl.AddressServiceImpl;
import com.qf.xiaomi.service.impl.CartServiceImpl;
import com.qf.xiaomi.service.impl.OrderServiceImpl;
import com.qf.xiaomi.web.base.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {

    CartService service = new CartServiceImpl();

    OrderService orderService = new OrderServiceImpl();

    AddressService addressService = new AddressServiceImpl();

    /**
     * 查询某用户的所有订单
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    public String getOrderList(HttpServletRequest req, HttpServletResponse resp ) throws  Exception {
        User user = (User) req.getSession().getAttribute("user");
        List<Order> orders = orderService.getOrderList(user.getId());
        req.setAttribute("orderList", orders);
        return "forward:/orderList.jsp";
    }

    //支付成功的 方法
    public String paySuccess(HttpServletRequest req, HttpServletResponse resp ) throws  Exception {
        String result = req.getParameter("result");
//        System.out.println(result);
        JSONObject jsonObject = JSON.parseObject(result);
        Object result1 = jsonObject.get("result");
        JSONObject jsonObject1 = JSON.parseObject(result1.toString());
        String return_code = jsonObject1.getString("return_code");
        String orderId = jsonObject1.getString("out_trade_no");
        //如果支付成功了, 更新订单的状态
        if("SUCCESS".equalsIgnoreCase(return_code)) {
            orderService.updateOrderStatus(orderId, "2");
        }

        return "redirect:/orderservlet?method=getOrderList";
    }


    /**
     * 提交订单
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    public String addOrder(HttpServletRequest req, HttpServletResponse resp ) throws  Exception {
        //获取 收货地址id
        Integer aid = Integer.parseInt(req.getParameter("aid"));
        User user = (User) req.getSession().getAttribute("user");

        Order order = orderService.addOrder(aid, user.getId());
        //把订单对象存储到请求域
        req.setAttribute("order", order);


        return "forward:/orderSuccess.jsp";
    }



    public String getOrderView(HttpServletRequest req, HttpServletResponse resp ) throws  Exception {
        //获取该用户的 购物车信息
        User user = (User) req.getSession().getAttribute("user");
        List<Cart> carts = service.getCarts(user.getId());

        //获取 地址列表
        List<Address> addList = addressService.getAddrList(user.getId());


        //把数据存储到 请求域
        req.setAttribute("carts", carts);
        req.setAttribute("addList", addList);
        //请求转发到 前端
        return "forward:/order.jsp";
    }
}
